package ie.defaoitigh;

import java.util.TreeMap;

public class GridReference {
	private char gridLetter;
	private int northing;
	private int easting;
	public TreeMap<Character, Integer> eastingGridLetter;
	public TreeMap<Character, Integer> northingGridLetter;

	public GridReference() {
		super();
		setupEastingGrid();
		setupNorthingGrid();
	}

	public GridReference(char gridLetter, int easting, int northing) {
		super();
		setupEastingGrid();
		setupNorthingGrid();
		this.gridLetter = gridLetter;
		this.northing = northing;
		this.easting = easting;
	}

	public String toString() {
		return new String(gridLetter + " " + easting + " " + northing);
	}
	
	public int getLongFormEasting(){
		
		return easting + eastingGridLetter.get(gridLetter);
	}
	
	public int getLongFormNorthing(){
		
		return northing + northingGridLetter.get(gridLetter);
	}

	public char getGridLetter() {
		return gridLetter;
	}

	public void setGridLetter(char gridLetter) {
		this.gridLetter = gridLetter;
	}

	public int getNorthing() {
		return northing;
	}

	public void setNorthing(int northing) {
		this.northing = northing;
	}

	public int getEasting() {
		return easting;
	}

	public void setEasting(int easting) {
		this.easting = easting;
	}
	
	public double getEastingDistanceBetween(GridReference dest){
		
		return java.lang.Math.sqrt(java.lang.Math.pow(getLongFormEasting()-dest.getLongFormEasting(),2)) * 100;
	}
	
	public double getNorthingDistanceBetween(GridReference dest){
		return java.lang.Math.sqrt(java.lang.Math.pow(getLongFormNorthing()-dest.getLongFormNorthing(),2)) * 100;
	}
	
	public double getBearingFrom(GridReference dest){
		
		double northings = getNorthingDistanceBetween(dest);
		double eastings = getEastingDistanceBetween(dest);
		double tan = northings/eastings;
		double deg = java.lang.Math.toDegrees(java.lang.Math.atan(tan));
		
		return deg + calcQuadrant(dest);
	}
	
	
	private double calcQuadrant(GridReference dest) {
		if((getLongFormEasting()-dest.getLongFormEasting()) >= 0){
			if((getLongFormNorthing()-dest.getLongFormNorthing()) >= 0){
				return 180;	
			} else {
				return 270;
			}
		} else {
			if((getLongFormNorthing()-dest.getLongFormNorthing()) >= 0){
				return 90;
			} else {
				return 0;
			}
		}
	}

	private void setupEastingGrid(){
		eastingGridLetter = new TreeMap<Character, Integer>();
		eastingGridLetter.put('A', 0);
		eastingGridLetter.put('F', 0);
		eastingGridLetter.put('L', 0);
		eastingGridLetter.put('Q', 0);
		eastingGridLetter.put('V', 0);
		
		eastingGridLetter.put('B', 1000);
		eastingGridLetter.put('G', 1000);
		eastingGridLetter.put('M', 1000);
		eastingGridLetter.put('R', 1000);
		eastingGridLetter.put('W', 1000);
		
		eastingGridLetter.put('C', 2000);
		eastingGridLetter.put('H', 2000);
		eastingGridLetter.put('N', 2000);
		eastingGridLetter.put('S', 2000);
		eastingGridLetter.put('X', 2000);
		
		eastingGridLetter.put('D', 3000);
		eastingGridLetter.put('J', 3000);
		eastingGridLetter.put('O', 3000);
		eastingGridLetter.put('T', 3000);
		eastingGridLetter.put('Y', 3000);
		
		eastingGridLetter.put('E', 4000);
		eastingGridLetter.put('K', 4000);
		eastingGridLetter.put('P', 4000);
		eastingGridLetter.put('U', 4000);
		eastingGridLetter.put('Z', 4000);
		
		
	}
	
	private void setupNorthingGrid(){
		northingGridLetter = new TreeMap<Character, Integer>();
		northingGridLetter.put('A', 4000);
		northingGridLetter.put('B', 4000);
		northingGridLetter.put('C', 4000);
		northingGridLetter.put('D', 4000);
		northingGridLetter.put('E', 4000);
		
		northingGridLetter.put('F', 3000);
		northingGridLetter.put('G', 3000);
		northingGridLetter.put('H', 3000);
		northingGridLetter.put('J', 3000);
		northingGridLetter.put('K', 3000);
		
		northingGridLetter.put('L', 2000);
		northingGridLetter.put('M', 2000);
		northingGridLetter.put('N', 2000);
		northingGridLetter.put('O', 2000);
		northingGridLetter.put('P', 2000);
		
		northingGridLetter.put('Q', 1000);
		northingGridLetter.put('R', 1000);
		northingGridLetter.put('S', 1000);
		northingGridLetter.put('T', 1000);
		northingGridLetter.put('U', 1000);
		
		northingGridLetter.put('V', 0);
		northingGridLetter.put('W', 0);
		northingGridLetter.put('X', 0);
		northingGridLetter.put('Y', 0);
		northingGridLetter.put('Z', 0);
		
		
	}

	public void setGridLetter(byte[] bytes) {
		setGridLetter((char) bytes[0]);
		
	}

}
